/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"AFF39BF0-E5AE-4613-A2C0-12D35002B8EA",variableType:4}
 */
var fUservDestino = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"D1356F57-0537-48C3-86CA-180349862232",variableType:4}
 */
var fUservFuente = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"5F89064D-D7FC-4098-8618-582264971A70"}
 */
var fServidor = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"8BD1C533-4EC4-4332-AE82-8B595E8E6F27"}
 */
var fTabla = null;

/**
 * Handle changed data.
 *
 * @param {String} oldValue old value
 * @param {String} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"A7B6629D-F15B-4073-BCB3-53C8BE6119F5"}
 */
function onDataChangeServidor(oldValue, newValue, event) {
	var $tablas = databaseManager.getTableNames(fServidor)
	application.setValueListItems('tablas',$tablas)
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"5100770F-3D03-4858-A355-E5606C9FEF3E"}
 */
function copiar(event) {
	databaseManager.setAutoSave(false)
	var $pk = databaseManager.getTable(fServidor,fTabla).getRowIdentifierColumnNames()
	var $fs = databaseManager.getFoundSet(fServidor,fTabla)
	
	var $sql = 'SELECT * FROM '+fTabla+ ' WHERE userv_id = ?'
	var $args = [fUservFuente]
	var $ds = databaseManager.getDataSetByQuery(fServidor,$sql,$args,-1)
	
	var $cant = $ds.getMaxRowIndex()
	var $cant_c = $ds.getMaxColumnIndex()
	
	for (var i = 1 ; i <= $cant ; i++)
	{
		$fs.newRecord()
		for (var j = 1 ; j <= $cant_c ; j++)
		{
			var $col = $ds.getColumnName(j)
			if ($pk.indexOf($col) == -1 && $col.indexOf('fecha_crea') == -1 && $col.indexOf('fecha_modif') == -1 && $col.indexOf('usuario_crea') == -1 && $col.indexOf('usuario_modif') == -1)
			{
				if ($col == 'userv_id')
				{
					$fs.userv_id = fUservDestino
				}
				else
				{
					$fs[$col] = $ds.getValue(i,j)
				}
			}
		}
	}
	databaseManager.startTransaction()
	if (databaseManager.saveData())
	{
		if (databaseManager.commitTransaction())
		{
			databaseManager.setAutoSave(true)
			globals.DIALOGS.showInfoDialog('Aviso','Proceso terminado con éxito','OK')
			return
		}
		else
		{
			databaseManager.rollbackTransaction()
			databaseManager.revertEditedRecords()
			databaseManager.setAutoSave(true)
			globals.DIALOGS.showWarningDialog('Aviso','Error al intentar grabar los datos','OK')
		}
	}
	else
	{
		databaseManager.rollbackTransaction()
		databaseManager.revertEditedRecords()
		databaseManager.setAutoSave(true)
		globals.DIALOGS.showWarningDialog('Aviso','Ha ocurrido un error en los datos','OK')
	}
}
